
#include "linked_network.h"
#include "first_title.h"


static lv_obj_t * btn;
static lv_obj_t * btn_label;
static uint8_t count_down = 3;
static lv_timer_t * timer_count_down;

static void enter_timer_1s()
{
    if(count_down)
    {
        count_down--;
        lv_label_set_text_fmt(btn_label,"进入 (%ds)", count_down);
    }
    else
    {
        debug_d("timeout\n");

         if(timer_count_down){
            lv_timer_del(timer_count_down);
            timer_count_down = NULL;
        }

        page_install("user_agreement",lv_page_user_agreement_create(),NULL);
        page_push_anim("user_agreement",LOAD_ANIM_MOVE_LEFT);
    }
}

static void enter_btn_event_callback(lv_event_t * event)
{
    if(event->code == LV_EVENT_CLICKED)
    {
        debug_d("test clicked\n");
        page_install("user_agreement",lv_page_user_agreement_create(),NULL);
        page_push_anim("user_agreement",LOAD_ANIM_MOVE_LEFT);
    }
}

void ui_init_linked_network_page_src(lv_obj_t *superview)
{
    first_title_set( FIRST_PAGE_NETWORKING, 0, 5);

    lv_obj_t * img = lv_img_create(superview);
    lv_img_set_src( img, pt_fs_get("Succeed.png"));
    lv_obj_align( img,LV_ALIGN_TOP_LEFT,186,79);

    lv_obj_t * label = lv_label_create(superview);
    lv_obj_set_style_text_font( label, lv_font_get(20), LV_STATE_DEFAULT );
    lv_obj_set_style_text_color(label,lv_color_make(0xff,0xff,0xff),LV_PART_MAIN);
    lv_obj_set_style_text_opa(label,LV_OPA_80,0);
    lv_label_set_text( label, "联网成功");
    lv_obj_align( label, LV_ALIGN_TOP_LEFT,181,168);

    btn = lv_btn_create(superview);
    lv_obj_set_style_radius(btn, LV_PCT(25), LV_PART_MAIN);
    lv_obj_set_style_outline_width(btn, 0, LV_PART_MAIN);
	lv_obj_set_style_shadow_width(btn, 0, LV_PART_MAIN);
    lv_obj_set_style_bg_color(btn,lv_color_make(0x26,0x7a,0xff),0);
    lv_obj_set_style_bg_opa(btn,LV_OPA_100,0);
    lv_obj_set_pos(btn,52,250);
    lv_obj_set_size(btn,350,50);

    btn_label = lv_label_create(btn);
    lv_obj_set_style_text_font( btn_label, lv_font_get(24), LV_STATE_DEFAULT );//字体大小
    lv_label_set_text(btn_label,"进入 (3s)");
    lv_obj_center(btn_label);

    lv_obj_add_event_cb(btn, enter_btn_event_callback, LV_EVENT_ALL, NULL);

    timer_count_down = lv_timer_create(enter_timer_1s, 1000, NULL);
}

static void linked_network_page_on_custom_attr_config(page_base_t* self)
{
    return ;
}
static void linked_network_page_on_view_load(page_base_t* self)
{
    ui_init_linked_network_page_src(self->root);
    return ;
}
static void linked_network_page_on_view_did_load(page_base_t* self)
{
    return ;
}
static void linked_network_page_on_view_will_appear(page_base_t* self)
{
    return ;
}
static void linked_network_page_on_view_did_appear(page_base_t* self)
{
    return ;
}
static void linked_network_page_on_view_will_disappear(page_base_t* self)
{
    return ;
}
static void linked_network_page_on_view_did_disappear(page_base_t* self)
{
    return ;
}
static void linked_network_page_on_view_did_unload(page_base_t* self)
{
    if(btn_label != NULL)
    {
        lv_obj_del(btn_label);
        btn_label = NULL;
    }

    if(timer_count_down != NULL)
    {
        lv_timer_del(timer_count_down);
        timer_count_down = NULL;
    }
    return ;
}

static page_vtable_t page_param = {
    .on_custom_attr_config  = linked_network_page_on_custom_attr_config,
    .on_view_load           = linked_network_page_on_view_load,
    .on_view_did_load       = linked_network_page_on_view_did_load,
    .on_view_will_appear    = linked_network_page_on_view_will_appear,
    .on_view_did_appear     = linked_network_page_on_view_did_appear,
    .on_view_will_disappear = linked_network_page_on_view_will_disappear,
    .on_view_did_disappear  = linked_network_page_on_view_did_disappear,
    .on_view_did_unload     = linked_network_page_on_view_did_unload,
};


page_vtable_t* lv_page_linked_network_create(void)
{
    return &page_param;
}
